import dayjs from 'dayjs'

export default {
  data() {
    return {}
  },
  methods: {
    formatNumber(num: string | number | null, toFix: number = 2): string {
      if (!num) {
        return (0).toFixed(toFix)
      } else {
        return Number(num).toFixed(toFix)
      }
    },
    simplifyNumber(num: number): string {
      if (num < 1000) {
        return num.toString()
      } else if (num < 10000) {
        return (num / 1000).toFixed(0) + '千'
      } else {
        return (num / 10000).toFixed(0) + '万'
      }
    },
    formmatTime(time: number, format: string = 'YYYY-MM-DD HH:mm:ss'): string {
      if (typeof time === 'number' && time.toString().length === 10) {
        return dayjs.unix(time).format(format)
      }
      return dayjs(time).format(format)
    }
  }
}
